package org.jeecg.modules.crawlerpaper.service.impl;


import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.mapper.CrawlerInfoMapper;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.List;

/**
 * @Description: crawler_info
 * @Author: jeecg-boot
 * @Date:   2021-07-22
 * @Version: V1.0
 */
@Service
public class CrawlerInfoServiceImpl extends ServiceImpl<CrawlerInfoMapper, CrawlerInfo> implements ICrawlerInfoService {

    @Autowired
    private ISysDictService sysDictService;

    @Override
    public void addCrawlerInfo(List<CrawlerInfo> crawlerInfoList){
        if(!CollectionUtils.isEmpty(crawlerInfoList)){
            List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
            if(!CollectionUtils.isEmpty(keywords)){

                Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                while (iterator.hasNext()){
                    CrawlerInfo next = iterator.next();
                    Boolean removeFlag = true;
                    for (DictModel keyword : keywords) {
                        if(next.getArticleName().contains(keyword.getValue())||next.getArticleContent().toString().contains(keyword.getValue())){
                            removeFlag = false;
                            break;
                        }
                    }
                    if(removeFlag){
                        iterator.remove();
                    }

                }
            }
            if(!CollectionUtils.isEmpty(crawlerInfoList)){
                this.saveOrUpdateBatch(crawlerInfoList);

            }

        }
    }


}
